var com = {
	mahees : {
		EventBus : function() {
			return _.extend({}, Backbone.Events);
		},
		views : {},
		models : {},
		collections : {},
		router : {}
	}
}

com.mahees.models.Project = Backbone.Model.extend({
	defaults : function() {
		return {
			name : '',
			displayImagePath : 'screenshot3.jpg',
			category : ''
		}
	}
});

com.mahees.models.Page = Backbone.Model.extend({
	defaults : function() {
		return {
			selected : ''
		}
	}
});

com.mahees.collections.Portfolio = Backbone.Collection.extend({
	model : com.mahees.models.Project
});

/*
 * com.mahees.views.TabCollectionView = Backbone.View.extend({
 * 
 * events : {},
 * 
 * initialize : function() { this.collection.bind('change', this.render); },
 * 
 * render : function() { if (this.model.get('selected')) { this.el.show(); } }
 * });
 */

com.mahees.views.MainView = Backbone.View.extend({
	el : 'body',

	events : {
		'click a.navigation' : 'navigationLinkClicked'
	},

	navigationLinkClicked : function(e) {
		e.preventDefault();
		var route = $(e.currentTarget).data("route");
		this.options.eventBus.trigger('navigate', route);
		return false;
	}
});

com.mahees.views.PageView = Backbone.View.extend({
	
	events : {},

	initialize : function() {
		_.bindAll(this, 'render');
		
		this.pageName = this.options.pageName;
		this.el = $('div#' + this.pageName + '-content');

		this.model.bind('change:selected', this.render);
	},

	render : function() {
		(this.model.get('selected') == this.pageName) ? this.el.show() : this.el.hide();
	}
});

com.mahees.views.PortfolioDetailView = Backbone.View.extend({
	el : 'div#portfolio-detail-content',

	events : {
		'click a#previous-portfolio-detail-content' : 'previousDetail',
		'click a#next-portfolio-detail-content' : 'nextDetail'
	},

	previousDetail : function(e) {
		var focused = this.getCurrentFocus();

		e.preventDefault();
		var route = focused.prev().data("route");
		this.options.eventBus.trigger('navigate', route);
		return false;
	},

	nextDetail : function(e) {
		var focused = this.getCurrentFocus();

		e.preventDefault();
		var route = focused.next().data("route");
		this.options.eventBus.trigger('navigate', route);
		return false;
	},

	getCurrentFocus : function() {
		return $('div:visible', this.el);
	}
});

com.mahees.router.ApplicationRouter = Backbone.Router.extend({

	routes : {
		'' : 'selectTab',
		'notFound/:tab' : 'notFound',
		'portfolio/:name' : 'detailPortfolio',
		'portfolio/:name/image/:id' : 'showImage',
		':tab' : 'selectTab'
	},

	initialize : function() {
		this.eventBus = new com.mahees.EventBus();
		this.eventBus.bind('navigate', function(url) {
			this.navigate(url, true)
		}, this);

		this.pageSelectedModel = new com.mahees.models.Page({
			selected : 'home'
		});

		new com.mahees.views.MainView({
			eventBus : this.eventBus,
			model : this.pageSelectedModel
		});

		new com.mahees.views.PortfolioDetailView({
			eventBus : this.eventBus,
			model : this.pageSelectedModel
		});

		this.contentContainer = $('#content-container');
		this.contentContainer.children().hide();
		this.portfolioDetailContent = $('#portfolio-detail-content', this.contentContainer);
		this.portfolioPhoto = $('#portfolio-large-photo', this.contentContainer);

		this.portfolioProjects = this.populatePortfolioCollection(window.portfolioProjects);

		this.tabsCollection = [];

		for (i in window.pageNames) {
			var pageName = window.pageNames[i];
			new com.mahees.views.PageView({
				model : this.pageSelectedModel,
				//el : $('div#' + pageName + '-content'),
				pageName : pageName
			});
		}

	},

	showImage : function(name, id) {
		this.contentContainer.children().hide();
		this.portfolioDetailContent.children('div').hide();
		this.portfolioPhoto.show();
	},

	detailPortfolio : function(name) {
		this.contentContainer.children().hide();
		this.portfolioDetailContent.children('div').hide();
		this.portfolioPhoto.hide();
		this.portfolioDetailContent.show();
		$('#' + name + '-detail').show();
	},

	selectTab : function(tab) {
		tab || (tab = 'home');

		this.pageSelectedModel.set({
			selected : tab
		});
	},

	notFound : function(tab) {
		alert(tab + ' resource not found');
		// this.navigate('', true);
	},

	populatePortfolioCollection : function(projectsJson) {
		var portfolio = new com.mahees.collections.Portfolio();

		for (projectCategory in projectsJson) {
			var categoryProjects = projectsJson[projectCategory];

			for (i in categoryProjects) {
				var projectJson = categoryProjects[i];
				portfolio.add(new com.mahees.models.Project({
					name : projectJson.name,
					category : projectCategory
				}));
			}
		}

		return portfolio;
	}

});